//
//  ClassViewController.m
//  MMLOOK
//
//  Created by dllo on 15/8/29.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "ClassViewController.h"
#import "ClassCollectionViewCell.h"
#import "AsynConnect.h"
#import "ObjectCollectionViewCell.h"
#import "SituAtionCollectionViewCell.h"
#import "StyleCollectionViewCell.h"
#import "PresentViewController.h"

@interface ClassViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collection;

@property (nonatomic, retain)NSMutableArray *classArray;
@property (nonatomic, retain)NSMutableArray *objectArray;
@property (nonatomic, retain)NSMutableArray *situArray;
@property (nonatomic, retain)NSMutableArray *styleArray;

@property (nonatomic, retain)NSMutableArray *BigArray;


@end

@implementation ClassViewController

- (void)dealloc
{
    [_BigArray release];
    
    
    [_classArray release];
    [_objectArray release];
    [_situArray release];
    [_styleArray release];
    [_collection release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.BigArray = [NSMutableArray arrayWithObjects:@"http://api.liwushuo.com/v1/channels/111/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/110/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/118/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/2/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/117/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/116/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/113/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/121/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/112/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/3/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/119/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/120/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/122/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/123/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/124/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/10/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/9/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/24/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/6/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/26/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/5/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/17/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/30/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/32/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/33/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/31/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/35/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/36/items?limit=20&offset=0", @" http://api.liwushuo.com/v1/channels/40/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/39/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/125/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/11/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/14/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/28/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/126/items?limit=20&offset=0", @"http://api.liwushuo.com/v1/channels/127/items?limit=20&offset=0",  nil];
        self.classArray = [NSMutableArray array];
        self.objectArray = [NSMutableArray array];
        self.situArray = [NSMutableArray array];
        self.styleArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self connection];
    self.view.backgroundColor = [UIColor cyanColor];
    [self creatCollcection];
}

- (void)creatCollcection
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(45, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 15;
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout] autorelease];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    _collection.contentSize = CGSizeMake(0, 700);
    [_collection registerClass:[ClassCollectionViewCell class] forCellWithReuseIdentifier:@"class"];
    [_collection registerClass:[ObjectCollectionViewCell class] forCellWithReuseIdentifier:@"object"];
    [_collection registerClass:[SituAtionCollectionViewCell class] forCellWithReuseIdentifier:@"situation"];
    [_collection registerClass:[StyleCollectionViewCell class] forCellWithReuseIdentifier:@"style"];
    _collection.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_collection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    
    switch (section) {
        case 0:
        {
            return _classArray.count;
        }
            break;
            case 1:
        {
            return _objectArray.count;
        }
            break;
            case 2:
        {
            return _situArray.count;
        }
            
            break;
            case 3:
        {
            return _styleArray.count;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"class" forIndexPath:indexPath];
        
            NSMutableDictionary *dic = [_classArray objectAtIndex:indexPath.row];
            [cell sendDic:dic];
       
        return cell;
    }
    
    if (indexPath.section == 1) {
        ObjectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"object" forIndexPath:indexPath];
        
            NSMutableDictionary *dic = [_objectArray objectAtIndex:indexPath.row];
            [cell sendDic:dic];
        
        
        return cell;
        
    }
    if (indexPath.section == 2) {
        SituAtionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"situation" forIndexPath:indexPath];
        
        
            NSMutableDictionary *dic = [_situArray objectAtIndex:indexPath.row];
            [cell sendDic:dic];
       
        
        return cell;
    }
    if (indexPath.section == 3) {
        StyleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"style" forIndexPath:indexPath];
        
            NSMutableDictionary *dic = [_styleArray objectAtIndex:indexPath.row];
            [cell sendDic:dic];
        
        
        return cell;
    }
    return nil;
}

- (void)connection
{
    [AsynConnect asynConnectWithUrlstr:@"http://api.liwushuo.com/v1/channel_groups/all" parmater:nil block:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littleDic objectForKey:@"channel_groups"];
        //class
        NSMutableDictionary *classDic = [arr objectAtIndex:0];
        NSMutableArray *class = [classDic objectForKey:@"channels"];
        [_classArray addObjectsFromArray:class];
        
        
        //object
        NSMutableDictionary *objectdic = [arr objectAtIndex:1];
        NSMutableArray *object = [objectdic objectForKey:@"channels"];
        [_objectArray addObjectsFromArray:object];
        
        //situation
        NSMutableDictionary *situDic = [arr objectAtIndex:2];
        NSMutableArray *situ = [situDic objectForKey:@"channels"];
        [_situArray addObjectsFromArray:situ];
        
        //style
        NSMutableDictionary *styleDic = [arr objectAtIndex:3];
        NSMutableArray *style = [styleDic objectForKey:@"channels"];
        [_styleArray addObjectsFromArray:style];
        
//
        
        [_collection reloadData];
    }];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PresentViewController *presen = [[[PresentViewController alloc] init] autorelease];
    
    if (indexPath.section == 0) {
        presen.str = [_BigArray objectAtIndex:indexPath.item];
    }else if (indexPath.section == 1){
        presen.str = [_BigArray objectAtIndex:indexPath.item + 15];
    }else if (indexPath.section == 2){
        presen.str = [_BigArray objectAtIndex:indexPath.item + 22];
    }else if (indexPath.section == 3){
        presen.str = [_BigArray objectAtIndex:indexPath.item +  30];
    }

    [self.navigationController pushViewController:presen animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
